package com.qianxx.base.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.LinearLayout.LayoutParams;

public final class ScreenUtil {

	private ScreenUtil() {
	};

	public static int getScreenWitdh(Context context) {
		WindowManager manager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		DisplayMetrics metrics = new DisplayMetrics();
		display.getMetrics(metrics);
		return metrics.widthPixels;
	}

	public static int getScreenHeight(Context context) {
		WindowManager manager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		DisplayMetrics metrics = new DisplayMetrics();
		display.getMetrics(metrics);
		return metrics.heightPixels;
	}

	private static float defaultDensity = 1.5f;// 高分辨率的手机density普遍接近1.5

	/**
	 * 根据手机的分辨率 dp 转成px(像素)
	 * */
	public static int dip2px(float dpValue, Context context) {
		int px;
		float density = context.getResources().getDisplayMetrics().density;
		if (density == 0) {
			px = (int) (dpValue * defaultDensity + 0.5f);
		} else {
			px = (int) (dpValue * density + 0.5f);
		}
		return px;
	}

	/**
	 * 根据手机的分辨率px(像素) 转成dp
	 */
	public static int px2dip(float pxValue, Context context) {
		int dp;
		float density = context.getResources().getDisplayMetrics().density;
		if (density == 0) {
			dp = (int) (pxValue / defaultDensity + 0.5f);
		} else {
			dp = (int) (pxValue / density + 0.5f);
		}
		return dp;
	}

	/**
	 * 获取屏幕n%的宽度
	 */
	public static int getWidth(Context context, int n) {
		int screenWidth = getScreenWitdh(context);
		return screenWidth * n / 100;
	}

	/**
	 * 获取屏幕n%的高度
	 */
	public static int getHeight(Context context, int n) {
		int screenHeight = getScreenHeight(context);
		return screenHeight * n / 100;
	}

	/**
	 * 获取屏幕n%宽度的LayoutParams
	 */
	public static LayoutParams getWidthParam(Context context, int n) {
		int screenWidth = getScreenWitdh(context);
		LayoutParams params = new LayoutParams(screenWidth * n / 100,
				LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.CENTER_HORIZONTAL;
		return params;
	}

	/**
	 * 获取屏幕n%高度的LayoutParams
	 */
	public static LayoutParams getHeightParam(Context context, int n) {
		int screenHeight = getScreenHeight(context);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				screenHeight * n / 100);
		params.gravity = Gravity.CENTER_VERTICAL;
		return params;
	}
}
